package com.itheima.ums.controller;

import cn.hutool.core.collection.CollUtil;
import com.itheima.mall.pojo.Menu;
import com.itheima.mall.vo.PageResult;
import com.itheima.ums.service.UmsMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class UmsMenuController {

    @Autowired
    private UmsMenuService umsMenuService;

    /**
     * 添加后台菜单
     *
     * @param menu
     * @return
     */
    @PostMapping("/create")
    public ResponseEntity createMenu(@RequestBody Menu menu) {

        Integer count = umsMenuService.createMenu(menu);
        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(count);
    }

    /**
     * 根据ID删除后台菜单
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public ResponseEntity deleteMenuById(@PathVariable("id") Long id) {

        Integer count = umsMenuService.deleteMenuById(id);
        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(count);
    }

    /**
     * 分页查询后台菜单
     *
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{parentId}")
    public ResponseEntity getMenuPage(@PathVariable("parentId") Long parentId, Integer pageNum, Integer pageSize) {
        PageResult pageResult = umsMenuService.getMenuPage(parentId, pageNum, pageSize);
        if (pageResult == null) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 修改后台菜单
     *
     * @param menu
     * @return
     */
    @PostMapping("/update/{id}")
    public ResponseEntity updateMenuById(@PathVariable("id") Long id, @RequestBody Menu menu) {
        Integer count = umsMenuService.updateMenuById(id, menu);

        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(count);
    }


    /**
     * 树形结构返回所有菜单列表
     *
     * @return
     */
    @GetMapping("/treeList")
    public ResponseEntity treeList() {

        List<Menu> vos = umsMenuService.treeList();
        if (CollUtil.isEmpty(vos)) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(vos);
    }

    /**
     * 根据id获取菜单详情
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity getMenuById(@PathVariable("id") Long id) {
        Menu menu = umsMenuService.getMenuById(id);
        if (menu == null) {
            return ResponseEntity.ok("ok");
        }

        return ResponseEntity.ok(menu);
    }

    /**
     * 修改菜单显示状态
     *
     * @param id
     * @param hidden
     * @return
     */
    @PostMapping("/updateHidden/{id}")
    public ResponseEntity updateHidden(@PathVariable("id") Long id,
                                       @RequestParam(name = "hidden") Long hidden) {
        Integer count = umsMenuService.updateHidden(id, hidden);

        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(count);

    }


}
